package com.hangwei.paperless.adapter;

import android.support.annotation.Nullable;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.DictBean;
import com.twelve.all.adapter.MyAdapter;
import com.twelve.all.utils.Tools;

import java.util.List;

/**
 * Created by 张龙臣 on 2020/12/16.
 */

public class TypeAdapter extends MyAdapter<DictBean, BaseViewHolder> {

    private final int mType;

    public TypeAdapter(@Nullable List<DictBean> data, int type) {
        super(R.layout.item_type, data);
        mType = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, DictBean item) {
        helper.setIsRecyclable(false);
        CheckBox checkbox = helper.getView(R.id.checkbox);
        TextView tvValue = helper.getView(R.id.tvValue);

        checkbox.setChecked(item.checked);
        tvValue.setTextColor(item.checked ? mContext.getResources().getColor(R.color.blue1) : mContext.getResources().getColor(R.color.text));
        tvValue.setText(Tools.checkedString( item.name));
        if (mType == 1) {
            helper.itemView.setOnClickListener(v -> {
                item.checked = !item.checked;
                notifyDataSetChanged();
            });
            checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                item.checked = isChecked;
                notifyDataSetChanged();
            });
        }else {
            helper.itemView.setOnClickListener(v -> {
                if (!item.checked) {
                    for (DictBean sh : mData) {
                        sh.checked = false;
                    }
                    item.checked = true;
                }else item.checked = false;
                notifyDataSetChanged();
            });
            checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (!item.checked) {
                    for (DictBean sh : mData) {
                        sh.checked = false;
                    }
                    item.checked = true;
                }else item.checked = false;
                notifyDataSetChanged();
            });
        }
    }
}
